home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 11 / AMUG BBS in a Box Volume XI (April 1994) (MacWizards).iso / Files / Prog / T / ToolsPlus 2.1.sit / Tools Plus 2.1 ƒ / Tools Plus 2.1 (C & Pascal) / User Manual / 16-Memory < prev    next >
Encoding:
Text File  |  1993-10-24  |  11.4 KB  |  200 lines  |  [TEXT/ttxt]

  1. 16  Memory
  2. ``````````
  3.  
  4. This chapter details the routines and processes in Tools Plus that consume memory.  All Tools Plus objects exist in your application’s heap (the memory you allocate to an application in the “Get Info” dialog under MultiFinder or System 7).  The only exception to this is a global record that is used by Tools Plus to keep its internal workings functioning.  The Tools Plus global record is placed on your application’s stack.  Fortunately, the global record is less than 2K, so it consumes relatively little stack space.
  5.  
  6. Almost all the objects created by Tools Plus routines are relocatable, and are therefore accessed internally by handles.  The use of relocatable objects insures that the largest contiguous memory block can be allocated when required.  The only non-relocatable objects created by Tools Plus are window records, but this is not a problem providing your application calls InitToolsPlus early in the program.  This allocates the window records low in memory where they won’t cause any heap fragmentation.
  7.  
  8. Each routine consumes stack memory just to execute its own code.  After the routine has completed execution, that memory is automatically reclaimed by the Memory Manager as it is needed.  The objects created by these routines (such as buttons, list boxes, etc.) remain in memory (the application heap) until they are explicitly deleted by a specific routine, or when your application ends.  Routines that consume memory only during their execution are not listed.
  9.  
  10. It is your responsibility to ensure that an operation can be completed with the amount of memory that is available to your application.  For example, testing can determine the exact amount to memory required to open a window with 5 buttons, two list boxes, and 40 editing fields.  Your application should ensure that the memory required to create such a window can be allocated safely before the WindowOpen statement is executed.
  11.  
  12.  
  13.  
  14.  
  15.  
  16. Measurements
  17. ````````````
  18. Measurements of memory consumption are approximate for two very important reasons.  Firstly, many objects contain handles to other objects, and because of this, they consume memory from handle blocks.  Handle blocks are a pool of handles that are available to any process or object that requires them.  When a process or object no longer requires a handle, the handle is released back into the available pool.  Additional memory will not be required providing that the demand for handles never exceeds the available supply.  As soon as the demand for handles exceeds the available supply, an additional block of 64 handles is automatically created, consuming 512 bytes of memory.  An entire block will be created even if only one more handle is needed.
  19.  
  20. The second reason approximate values are provided instead of exact values, is because the Macintosh’s various managers are not always consistent in the way they allocate memory.  The List Manager, for example, needs over 5K the first time any of its routines are accessed.  Once that 6K kernel is loaded, memory consumption is nominal.  Also, the memory consumed by various processes will likely vary between versions of the System.
  21.  
  22.  
  23.  
  24.  
  25.  
  26. Routines that consume heap space (by category)
  27. ``````````````````````````````````````````````
  28. The following routines create objects in memory, thereby consuming heap space:
  29.  
  30.   Program Initialization: InitToolsPlus
  31.  
  32.                  Windows: WindowOpen
  33.                           WindowTitle
  34.  
  35.                  Buttons: NewButton
  36.                           ButtonTitle
  37.  
  38.              Scroll Bars: NewScrollBar
  39.  
  40.            Editing Field: NewField
  41.                           ActivateField (if a field is not already
  42.                                          active)
  43.                           ClickInField (if a field is not already
  44.                                         active)
  45.  
  46.               List Boxes: NewListBox
  47.                           SetListBoxText
  48.                           InsertListBoxLine
  49.  
  50.                   Menus:  AppleMenu
  51.                           Menu
  52.                           InsertMenuItem
  53.                           RenameItem
  54.  
  55.                 Cursors: CursorShape
  56.                          ResetCursor
  57.                          NewCursorTable
  58.                          CursorZone
  59.  
  60.        Color Drawing/
  61.        Multiple Screens: NumberOfScreens
  62.                          BeginUpdateScreen
  63.                          DrawIcon
  64.  
  65.  
  66.  
  67.  
  68.  
  69. Routines that consume heap space (alphabetic)
  70. `````````````````````````````````````````````
  71. The following section alphabetically lists routines that create objects in memory.  You will likely notice that in most cases, the amount of memory consumed is negligible.
  72.  
  73. ActivateField     When an editing field is activated, it consumes 8
  74.                   bytes of memory, plus 1 byte for each character of
  75.                   edited text.  When the editing field is deactivated,
  76.                   either by deactivating it, activating another field,
  77.                   clicking in another field, or closing the window, this
  78.                   memory is released.
  79.  
  80. AppleMenu         The Apple menu requires about 4K bytes of memory.
  81.  
  82. BeginUpdateScreen BeginUpdateScreen allocated about 40 bytes.  This
  83.                   memory is deallocated when EndUpdateScreen is called.
  84.  
  85. ButtonTitle       Each character of the button’s title string consumes 1
  86.                   byte of memory.  Memory consumed by any previous title
  87.                   is released.  Deleting the button, or closing its
  88.                   window releases its memory.
  89.  
  90. ClickInField      When an editing field is activated by ClickInField, it
  91.                   consumes an additional 8 bytes of memory, plus 1 byte
  92.                   for each character of edited text.  When the editing
  93.                   field is deactivated, either by deactivating it,
  94.                   activating another field, clicking in another field,
  95.                   or closing the window, this memory is released.
  96.  
  97. CursorShape       When a cursor is displayed the first time, the CURS
  98.                   resource is loaded into memory consuming 16 bytes of
  99.                   memory.  If your application has an animated cursor,
  100.                   the acur resource is also loaded, consuming 4 bytes
  101.                   plus an additional 4 bytes per animation frame.
  102.                      If a resource is flagged as “purgeable” it is
  103.                   automatically unloaded to reclaim memory when needed.
  104.  
  105. CursorZone        Each cursor zone requires about 28 bytes of memory.
  106.                   Deleting a cursor zone releases its memory.
  107.  
  108. DrawIcon          When an icon is displayed the first time, the required
  109.                   resource (cicn, icl8, icl4, ICN#, or ICON) is loaded
  110.                   into memory.  Depending on your icon family, this may
  111.                   consume as much as 2K of memory.
  112.                      If a resource is flagged as “purgeable” it is
  113.                   automatically unloaded to reclaim memory when needed.
  114.  
  115. InitToolsPlus     Multiply the value of MaxWindows by 300 bytes to
  116.                   determine the amount of memory consumed by windows.
  117.                   This amount is fixed, regardless if those windows are
  118.                   ever opened by your application.  About 40k is
  119.                   consumed by loading the code segments that contain
  120.                   Tools Plus’s libraries.
  121.  
  122. InsertListBoxLine Each line in a list box requires about 4 bytes of
  123.                   memory, plus 1 byte per character of text displayed in
  124.                   the line.  Deleting the line in the list box, deleting
  125.                   its containing list box, or closing its window
  126.                   releases the memory used by the line.
  127.  
  128. InsertMenuItem    Each menu item requires about 10 bytes of memory plus
  129.                   the length of the item’s title.  Deleting an item
  130.                   releases its memory.
  131.  
  132. Menu              Each menu requires about 36 bytes of memory.  Each
  133.                   menu item requires an additional 10 bytes plus the
  134.                   length of the item’s title.  Deleting the menu
  135.                   releases its memory and that of its associated items.
  136.                   Deleting an item releases its memory.
  137.  
  138. NewButton         Each button requires about 50 bytes of memory, plus
  139.                   the length of the button’s title.  Deleting the
  140.                   button, or closing its window releases its memory.
  141.  
  142. NewCursorTable    Each cursor table requires about 20 bytes of memory.
  143.                   Deleting a cursor table releases its memory and that
  144.                   of its associated zones.
  145.  
  146. NewField          Each editing field requires about 52 bytes of memory,
  147.                   regardless of the number of characters used by the
  148.                   field’s string.  Deleting the editing field, or
  149.                   closing its window releases its memory.
  150.  
  151. NewListBox        Each list box requires about 300 bytes of memory.  The
  152.                   first list box created will consume an additional 6K
  153.                   which is required to load the List Manager.  Deleting
  154.                   the list box, or closing its window releases its
  155.                   memory and the memory used by its associated lines.
  156.  
  157. NewScrollBar      Each scroll bar requires about 70 bytes of memory.
  158.                   Deleting the scroll bar, or closing its window
  159.                   releases its memory.
  160.  
  161. NumberOfScreens   The amount of memory consumed by NumberOfScreens
  162.                   depends on the number of monitors present when your
  163.                   application runs, and their settings.  In the worst
  164.                   case, about 40 bytes are consumed per monitor.
  165.                      Note that the amount of memory consumed is not
  166.                   incremented each time NumberOfScreens is called.
  167.  
  168. RenameItem        When a menu item is renamed, it consumes 1 byte plus
  169.                   the length of the item’s title.  The memory consumed
  170.                   by the previous name is released.  Deleting the menu
  171.                   releases its memory and that of its associated items.
  172.                   Deleting an item releases its memory.
  173.  
  174. ResetCursor       When a cursor is displayed the first time, the CURS
  175.                   resource is loaded into memory consuming 16 bytes of
  176.                   memory.  If a resource is flagged as “purgeable” it is
  177.                   automatically unloaded to reclaim memory when needed.
  178.  
  179. SetListBoxText    Each line in a list box requires about 4 bytes of
  180.                   memory, plus 1 byte per character of text displayed in
  181.                   the line.  Deleting the line in the list box, deleting
  182.                   its containing list box, or closing its window
  183.                   releases the memory used by the line.
  184.  
  185. WindowOpen        Each open window consumes an additional 300 bytes.
  186.                   Closing the window releases the memory.
  187.  
  188. WindowTitle       Each character of the window’s title string consumes 1
  189.                   byte of memory.  Memory consumed by any previous title
  190.                   is released.  Closing the window releases title
  191.                   memory.
  192.  
  193.  
  194.  
  195.  
  196.  
  197. Good memory habits
  198. ``````````````````
  199. It’s a very good idea to check the amount of available contiguous memory before allocating objects or calling a routine that may need to be loaded from disk (ie: a segment that is not yet in memory).  Do the same when you open a document too, because there are a number of applications out there that bomb just because a user created a document on a 8-meg Mac and tried to open it on another Mac with less memory.  Users perceive this type of behavior as being indicative of an unreliable program (it keeps bombing on me for no reason).
  200.